package com.suitsoft.ut.web.select;



import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.suitsoft.ut.commons.Constants;
import com.suitsoft.ut.commons.LogAnnotation;
import com.suitsoft.ut.entity.base.Student;
import com.suitsoft.ut.entity.thesis.Thesis;
import com.suitsoft.ut.entity.thesis.ThesisFile;
import com.suitsoft.ut.service.base.StudentManager;
import com.suitsoft.ut.service.thesis.ThesisFileManager;
import com.suitsoft.ut.web.CrudActionSupport;
import com.suitsoft.ut.web.InitListActionSupport;

/**
 * 解除选题与学生关系
 * <pre>把学生从 已确定学生的题目中移除</pre>
 *@author Suineg
 */
@Results({@Result(name = CrudActionSupport.RELOAD,location="cancel-select",type="redirect")})
public class CancelSelectAction extends InitListActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = -561798433635559813L;

	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	private ThesisFileManager fileManager;
	private StudentManager studentManager;

	private Page<Thesis> page = new Page<Thesis>(Constants.PAGE_SIZE);
	/** 教师名 */
	private String teacherName;
	/** 学生名 */
	private String studentName;
	/** 题目名 */
	private String titile;
	/** 题实体 */
	private Thesis thesis ;
	/** 流程实体 */
	private ThesisFile thesisFile;
	
	@Override
	public String execute() {
		page = thesisManager.findByConfirmStudent(page,teacherName,studentName,titile);
		return SUCCESS;
	}
	@LogAnnotation(log = "解除选题与选题学生关系 - 解除关系")
	public String cancel(){
		if(id == null || id == 0l){
			addActionMessage("非法操作！");
			return RELOAD;
		}
		try {
			thesis = thesisManager.get(id);
			for(Student s : thesis.getStudents()){
				s.setThesis(null);
				studentManager.save(s);
			}
			thesis.setStudentName(null);
			thesis.setStudentNo(null);
			thesisManager.save(thesis);
			
			thesisFile = thesis.getThesisFile();
			thesisFile.setThesisFlag(Constants.THESIS_FLAG_LITI);
			thesisFile.setThesisStatus(Constants.THSIS_STAUTS_LITI_FIRTHE_AUDIT);
			
			fileManager.save(thesisFile);
		} catch (Exception e) {
			e.printStackTrace();
			addActionMessage("操作失败！");
			return RELOAD;
		}
		addActionMessage("操作成功！");
		return RELOAD;
	}
	public Page<Thesis> getPage() {
		return page;
	}
	public void setPage(Page<Thesis> page) {
		this.page = page;
	}
	public String getTeacherName() {
		return teacherName;
	}
	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
	public String getTitile() {
		return titile;
	}
	public void setTitile(String titile) {
		this.titile = titile;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Thesis getThesis() {
		return thesis;
	}
	public void setThesis(Thesis thesis) {
		this.thesis = thesis;
	}
	public ThesisFile getThesisFile() {
		return thesisFile;
	}
	public void setThesisFile(ThesisFile thesisFile) {
		this.thesisFile = thesisFile;
	}
	@Autowired
	public void setFileManager(ThesisFileManager fileManager) {
		this.fileManager = fileManager;
	}
	@Autowired
	public void setStudentManager(StudentManager studentManager) {
		this.studentManager = studentManager;
	}
	
}

